package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.annotation.AutoFill;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.enumeration.OperationType;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageBean;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.Value;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    SetmealMapper setmealMapper;
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    SetmealDishMapper setmealDishMapper;
    @Autowired
    DishMapper dishMapper;

    /**
     * 根据分类id查询套餐
     *
     * @param categoryId  分类id
     * @return
     */
    public List<Setmeal> list(Long categoryId) {
        //封装起来
        Setmeal setmeal = new Setmeal();
        setmeal.setCategoryId(categoryId);
        //必须是起售中的
        setmeal.setStatus(StatusConstant.ENABLE);
        //查询
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;

    }

    /**
     * 根据id查询菜品选项
     *
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }


    /**
     * 套餐的分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageBean<SetmealVO> selectByPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.pageById(setmealPageQueryDTO);
        return new PageBean<>(page.getTotal(), page.getResult());

    }

    /**
     * 根据id查套餐
     *
     * @param id
     * @return
     */
    @Override
    public SetmealVO selectById(Long id) {
        //查询套餐表
        SetmealVO setmeal = setmealMapper.selectById(id);
        //查询中间表
        List<SetmealDish> list = setmealDishMapper.selectBySetmealId(id);
        //数据轨道SetmealVO中
        setmeal.setSetmealDishes(list);
        return setmeal;
    }

    /**
     * 新增一个套餐
     *
     * @param setmealDTO 新增的内容
     */
    @Override
    @AutoFill(value = OperationType.INSERT)
    @CacheEvict(cacheNames = "setmeal",allEntries = true)
    public void add(SetmealDTO setmealDTO) {

        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        //设置店铺状态
        setmeal.setStatus(0);
        setmealMapper.add(setmeal);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDTO.getId());
        }
        setmealDishMapper.add(setmealDishes);
    }

    /**
     * 更新套餐
     *
     * @param dto 更新套餐
     */
    @Override
    @CacheEvict(cacheNames = "setmeal",allEntries = true)
    public void update(SetmealDTO dto) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto, setmeal);
        //更新套餐表
        setmealMapper.update(setmeal);
        //删除就得菜品套餐中间表
        setmealDishMapper.deleteBySetmealId(dto.getId());
        //补全setmealId

        List<SetmealDish> setmealDishes = dto.getSetmealDishes();

        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(dto.getId());
        }
        //更新菜品套餐中间表
        setmealDishMapper.add(setmealDishes);


    }

    /**
     * 删除套餐
     *
     * @param ids
     */
    @Override
    @CacheEvict(cacheNames = "setmeal",allEntries = true)
    public void delete(List<Long> ids) {
        for (Long id : ids) {
            //删除菜品套餐中间表
            setmealDishMapper.deleteBySetmealId(id);
            //删除套餐
            setmealMapper.delete(id);
        }
    }

    /**
     * 更改套餐状态
     *
     * @param status 状态
     * @param id     id
     */
    @Override
    public void changeStatus(Integer status, Long id) {
        Setmeal build = Setmeal.builder().status(status)
                .id(id).build();
        //检查套餐下的每一个菜品的状态
        List<SetmealDish> list = setmealDishMapper.selectBySetmealId(id);
        for (SetmealDish setmealDish : list) {
            Long dishId = setmealDish.getDishId();
            Dish dish = dishMapper.getById(dishId);
            Integer s = dish.getStatus();
            //状态为0就抛出异常
            if (s == 0) {
                throw new RuntimeException(String.format(MessageConstant.SETMEAL_ENABLE_FAILED, dish.getName()));
            }

        }
        setmealMapper.updateStatus(build);
    }
}
